package com.ww.word.ui.more;

import java.util.ArrayList;
import java.util.List;

import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.ww.core.activity.MyActivity;
import com.ww.core.util.CoreSharedPreferencesHelper;
import com.ww.core.util.ImageUtil;
import com.ww.core.util.Logger;
import com.ww.word.R;
import com.ww.word.adapter.SkinAdapter;
import com.ww.word.entity.Skin;

/**
 * 主题修改界面. 
 * @version 2012-11-9
 * @author wangw
 */
public class SkinActivity extends MyActivity {
	private String themeName[];
	private String themeImage[];
	private String themeKey[];

	private ListView mListView;
	private SkinAdapter mSkinListAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_skin);

		Resources res = getResources();
		themeName = res.getStringArray(R.array.theme_name);
		themeImage = res.getStringArray(R.array.theme_image);
		themeKey = res.getStringArray(R.array.theme_key);

		setTitle("皮肤设置");
		findView();
		setIndex();
	}

	/**
	 * 初始化界面控件.
	 */
	private void findView() {
		mListView = (ListView) findViewById(R.id.themelist);
		mSkinListAdapter = new SkinAdapter(this, getItemList());
		mListView.setAdapter(mSkinListAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				mSkinListAdapter.setSelect(position);
				CoreSharedPreferencesHelper helper = new CoreSharedPreferencesHelper(SkinActivity.this);
				helper.setTheme(themeKey[position]);
				setTitle("皮肤设置");
			}
		});
	}

	/**
	 * 设置选中的主题.
	 */
	public void setIndex() {
		int index = 0;
		CoreSharedPreferencesHelper helper = new CoreSharedPreferencesHelper(SkinActivity.this);
		String theme = helper.getTheme();
		for (int i = 0; i < themeKey.length; i++) {
			if (theme.equals(themeKey[i])) {
				index = i;
				break;
			}
		}
		mSkinListAdapter.setSelect(index);
	}

	/**
	 * 组装主题列表.
	 * @return List<Skin>
	 */
	private List<Skin> getItemList() {
		List<Skin> list = new ArrayList<Skin>();
		for (int i = 0; i < themeName.length; i++) {
			Skin data = new Skin();
			Logger.info("themeName:"+themeImage[i]);
			data.setmImageViewLeftID(ImageUtil.getResource(this, themeImage[i]));
			data.setmTextView(themeName[i]);
			list.add(data);
		}
		return list;
	}
}
